package com.fyh.service.impl;

import com.fyh.entity.chat.ChatPageProp;
import com.fyh.entity.chat.ChatRecord;
import com.fyh.entity.chat.InformRecord;
import com.fyh.entity.chat.SimpleContact;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.feign.ChatJobFeignClient;
import com.fyh.feign.ChatUserFeignClient;
import com.fyh.mapper.ChatMapper;
import com.fyh.service.ChatService;
import com.fyh.ws.ChatWebSocketHandler;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ChatServiceImpl implements ChatService {

    @Autowired
    ChatMapper chatMapper;
    @Qualifier("com.fyh.feign.ChatUserFeignClient")
    @Autowired
    ChatUserFeignClient chatUserFeignClient;
    @Qualifier("com.fyh.feign.ChatJobFeignClient")
    @Autowired
    ChatJobFeignClient chatJobFeignClient;

    private static final Integer PAGE_SIZE = 10;

    @Autowired
    @Lazy
    ChatWebSocketHandler chatWebSocketHandler;


    /**
     * 获取联系人列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<SimpleContact> getSimpleContactList(Long userId) {
        // 查询联系人id和备注
        List<SimpleContact> simpleContactList = chatMapper.getSimpleContactList(userId);
        // 获取联系人头像链接
        simpleContactList = chatUserFeignClient.getContactListAvatarPath(simpleContactList).getData();
        // 获取联系人未读消息数
        for(SimpleContact simpleContact : simpleContactList){
            Integer unreadMessageCount = chatMapper.getUnreadMessageCount(simpleContact.getContactUserId(), userId);
            simpleContact.setUnreadMessageCount(unreadMessageCount > 99 ? 99 : unreadMessageCount);
        }
        return simpleContactList;
    }

    /**
     * 记录聊天记录
     *
     * @param chatRecord
     */
    @Override
    public void insertChatRecord(ChatRecord chatRecord) {
        log.info("保存聊天记录: {}", chatRecord);
        chatMapper.insertChatRecord(chatRecord);
    }

    /**
     * 更新聊天记录状态
     *
     * @param sendId
     * @param receiverId
     */
    @Override
    public void updateChatRecordStatus(Long sendId, Long receiverId) {
        ChatRecord chatRecord = new ChatRecord();
        chatRecord.setSenderId(sendId);
        chatRecord.setReceiverId(receiverId);
        chatRecord.setStatus(1);
        chatMapper.updateChatRecordStatus(chatRecord);
    }

    /**
     * 获取聊天记录列表
     *
     * @param chatPageProp
     * @return
     */
    @Override
    public List<ChatRecord> getChatRecordList(ChatPageProp chatPageProp) {
        chatPageProp.setSize(PAGE_SIZE);
        List<ChatRecord> chatRecordList = chatMapper.getChatRecordList(chatPageProp);
        return chatRecordList;
    }

    /**
     * 添加联系人
     *
     * @param contactId
     */
    @Override
    public void addChatContact(Long userId, Long contactId) {
        // 查询用户昵称
        String userName = chatUserFeignClient.getNickname(userId).getData();
        String contactName = chatUserFeignClient.getNickname(contactId).getData();
        // 添加联系人
        chatMapper.addChatContact(userId, userName, contactId, contactName, LocalDateTime.now());
    }

    /**
     * 获取用户联系人ID列表
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> getUserContactIdList(Long userId) {
        List<Long> contactIdList = chatMapper.getUserContactIdList(userId);
        return contactIdList;
    }

    /**
     * 获取用户是否有未读消息
     *
     * @param userId
     * @return
     */
    @Override
    public Boolean getUnreadMessage(Long userId) {
        return chatMapper.getUnreadMessage(userId);
    }

    /**
     * 更新联系人备注
     *
     * @param simpleContact
     */
    @Override
    public void updateContactName(SimpleContact simpleContact, Long userId) {
        String newContactName = simpleContact.getContactName();
        Long contactId = simpleContact.getContactUserId();
        chatMapper.updateContactName(userId, contactId, newContactName);
    }

    /**
     * 发送职位投递消息
     *
     * @param positionId
     * @param employerId
     */
    @Override
    public void sendDeliveryMessage(Long positionId, Long employerId) {
        chatWebSocketHandler.sendDeliveryMessage(positionId, employerId);
    }

    /**
     * 获取用户通知记录
     *
     * @param userId
     * @return
     */
    @Override
    public List<InformRecord> getUserInformRecord(Integer offset, Long userId) {
        PageHelper.offsetPage(offset, PAGE_SIZE);
        List<InformRecord> informRecordList = chatMapper.getUserInformRecord(userId);
        return informRecordList;
    }

    /**
     * 获取未读通知数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer getUnreadInform(Long userId) {
        Integer unreadInform = chatMapper.getUnreadInform(userId);
        return unreadInform > 99 ? 99 : unreadInform;
    }

    /**
     * 发送职位投递状态通知
     *
     * @param deliveryRecord
     */
    @Override
    @Transactional
    public void sendDeliveryStatusInform(DeliveryRecord deliveryRecord) {
        //获取职位名称
        String positionName = chatJobFeignClient.getJobPositionDetail(deliveryRecord.getPositionId()).getData().getPositionName();
        //调用Endpoint里的静态方法发起对在线用户的推送
        chatWebSocketHandler.sendInform(deliveryRecord.getUserId());
        //保存通知记录
        InformRecord informRecord = new InformRecord();
        informRecord.setReceiverId(deliveryRecord.getUserId());
        //感兴趣interested、不合适inappropriate
        String status = deliveryRecord.getStatus().equals("interested") ? "感兴趣" : "不合适";
        informRecord.setMessage("您投递的职位【" + positionName + "】已被HR处理，标记为【" + status + "】");
        //设置通知时间
        informRecord.setReceiveTime(LocalDateTime.now());
        chatMapper.insertInformRecord(informRecord);
    }

    /**
     * 更新通知状态为已读
     *
     * @param userId
     */
    @Override
    public void updateInformStatus(Long userId) {
        chatMapper.updateInformStatus(userId);
    }
}
